// ignore_for_file: unnecessary_null_comparison
import 'package:video_setting/pages/utils/unicom_channel/uicom_method_channel.dart';
import 'package:video_setting/pages/video_setting/event_bus.dart';
import 'package:video_setting/pages/video_setting/lock/unicom_lock_factory.dart';
import 'package:video_setting/pages/video_setting/lock/unicom_synchronized.dart';

class UnicomBleServiceStates {
  static const int ON = 1;
  static const int OFF = 2;
}

class UnicomBleService {
  static const String BLE_INFO_LIST_SCANF_EVENT = 'ble_info_list_scanf_event';
  static const String BLE_INFO_LIST_SCANF_COMPLETED_EVENT = 'ble_info_list_scanf_completed_event';
  static const String BLE_INFO_CONNECT_CHANGE_STATE_EVENT = 'ble_info_connect_change_event';
  static const String BLE_INFO_XUNJIAN_INFO_EVENT = 'ble_info_xunjian_info_event';

  /* common */
  static const String FLUTTER_CHANENL_NAME = 'unicom_bt_channel';
  /* ble  channel*/
  static const String _START_SCANF = 'start_scanf';
  static const String _STOP_SCANF = 'stop_scanf';
  static const String _START_CONNECT_BLE = 'start_connect_ble';
  static const String _DISCONNECT_BLE = 'disconnect_ble';
  static const String _VERIFY_PWD_BY_BLE = "verify_pwd";
  static const String _START_COLLECTION = "start_collection";
  static const String _GET_BT_STATUS = "getBTStatus";
  static const String _OPEN_NATIVE_BT = "openNativeBT";

  static const String _GET_BLE_INFO_LIST_FROM_BLE = 'get_ble_info_list_from_ble';
  static const String _GET_SCANF_RESULT_FROM_BLE = 'get_scanf_result_form_ble';
  static const String _GET_BLE_CONNECT_STATE = 'get_ble_connect_state';
  static const String _GET_XUNJIAN_EAR = 'get_ble_xunjian_ear';

  final UnicomMethodChannel _methodChannel;

  Lock _lock = UnicomBasicLockFactory().newLock();
  static UnicomBleService? _instance;

  factory UnicomBleService() {
    return _instance ??= UnicomBleService._private();
  }

  UnicomBleService._private() : _methodChannel = UnicomMethodChannel(FLUTTER_CHANENL_NAME) {
    _setMethodCallHandler();
  }

  Future<bool> getBTStatus() {
    return _lock.synchronized(() {
      return _methodChannel.callNative(_GET_BT_STATUS, null).then((value) => value);
    });
  }

  Future<bool> openNativeBle() {
    return _lock.synchronized(() {
      return _methodChannel.callNative(_OPEN_NATIVE_BT, null).then((value) => value);
    });
  }

  Future<bool> startScanf() {
    return _lock.synchronized(() {
      return _methodChannel.callNative(_START_SCANF, null).then((value) {
        return true;
      });
    });
  }

  Future<bool> stopScanf() {
    return _lock.synchronized(() {
      return _methodChannel.callNative(_STOP_SCANF, null).then((value) => value);
    });
  }

  Future<bool> connectToBle(String address) {
    return _lock.synchronized(() {
      return _methodChannel.callNative(_START_CONNECT_BLE, address).then((value) => value);
    });
  }

  Future<bool> disConnectToBle() {
    print("调用断开链接蓝牙");
    return _lock.synchronized(() {
      return _methodChannel.callNative(_DISCONNECT_BLE, null).then((value) => value);
    });
  }

  Future<bool> verifyPwd() {
    return _lock.synchronized(() {
      return _methodChannel.callNative(_VERIFY_PWD_BY_BLE, null).then((value) => value);
    });
  }

  Future<bool> startCollection() {
    return _lock.synchronized(() {
      return _methodChannel.callNative(_START_COLLECTION, null).then((value) => value);
    });
  }

  _setMethodCallHandler() {
    _methodChannel.setListener((call) async {
      if (call.method == _GET_BLE_INFO_LIST_FROM_BLE) {
        print("触发蓝牙列表查询");
        //获取当前ble info list, 逐个搜索出来
        if (call.arguments != null) {
          bus.emit(BLE_INFO_LIST_SCANF_EVENT, call.arguments);
        }
      } else if (call.method == _GET_SCANF_RESULT_FROM_BLE) {
        print("蓝牙列表查询完成");
        if (call.arguments != null) {
          bus.emit(BLE_INFO_LIST_SCANF_COMPLETED_EVENT, call.arguments);
        }
      } else if (call.method == _GET_BLE_CONNECT_STATE) {
        print("蓝牙链接状态发生改变 state= ${call.arguments}");
        if (call.arguments != null) {
          bus.emit(BLE_INFO_CONNECT_CHANGE_STATE_EVENT, call.arguments);
        }
      } else if (call.method == _GET_XUNJIAN_EAR) {
        print("巡检数据 state= ${call.arguments}");
        if (call.arguments != null) {
          bus.emit(BLE_INFO_XUNJIAN_INFO_EVENT, call.arguments);
        }
      }
    });
  }
}
